
using System;
using System.Collections;
using System.Collections.Generic;
using Velocity4Net.Runtime.Parse.AST;

namespace Velocity4Net.Runtime.Parse
{
    public class JJTParserState
    {
        private Stack<INode> nodes;
        private Stack<int> marks;

        private int mk;        // current mark
        private bool node_created;

        public JJTParserState()
        {
            nodes = new Stack<INode>();
            marks = new Stack<int>();
            mk = 0;
        }

        /* Determines whether the current node was actually closed and
           pushed.  This should only be called in the final user action of a
           node scope.  */
        public bool NodeCreated()
        {
            return node_created;
        }

        /* Call this to reinitialize the node stack.  It is called
           automatically by the parser's ReInit() method. */
        public void Reset()
        {
            nodes.Clear();
            marks.Clear();
            mk = 0;
        }

        /* Returns the root node of the AST.  It only makes sense to call
           this after a successful parse. */
        public INode RootNode()
        {
            return nodes.ToArray()[0];
        }

        /* Pushes a node on to the stack. */
        public void PushNode(INode n)
        {
            nodes.Push(n);
        }

        /* Returns the node on the top of the stack, and remove it from the
           stack.  */
        public INode PopNode()
        {
            if (nodes.Count - 1 < mk)
            {
                mk = marks.Pop();
            }
            return nodes.Pop();
        }

        /* Returns the node currently on the top of the stack. */
        public INode PeekNode()
        {
            return nodes.Peek();
        }

        /* Returns the number of children on the stack in the current node
           scope. */
        public int NodeArity()
        {
            return nodes.Count - mk;
        }


        public void ClearNodeScope(INode n)
        {
            while (nodes.Count > mk)
            {
                PopNode();
            }
            mk = marks.Pop();
        }


        public void OpenNodeScope(INode n)
        {
            marks.Push(mk);
            mk = nodes.Count;
            n.Open();
        }


        /* A definite node is constructed from a specified number of
           children.  That number of nodes are popped from the stack and
           made the children of the definite node.  Then the definite node
           is pushed on to the stack. */
        public void CloseNodeScope(INode n, int num)
        {
            mk = marks.Pop();
            while (num-- > 0)
            {
                INode c = PopNode();
                c.jjtSetParent(n);
                n.jjtAddChild(c, num);
            }
            n.Close();
            PushNode(n);
            node_created = true;
        }


        /* A conditional node is constructed if its condition is true.  All
           the nodes that have been pushed since the node was opened are
           made children of the conditional node, which is then pushed
           on to the stack.  If the condition is false the node is not
           constructed and they are left on the stack. */
        public void CloseNodeScope(INode n, bool condition)
        {
            if (condition)
            {
                int a = NodeArity();
                mk = marks.Pop();
                while (a-- > 0)
                {
                    INode c = PopNode();
                    c.jjtSetParent(n);
                    n.jjtAddChild(c, a);
                }
                n.Close();
                PushNode(n);
                node_created = true;
            }
            else
            {
                mk = marks.Pop();
                node_created = false;
            }
        }
    }
}
